package admin

import (
	"exercise/application/yiran/app/libs"
	"exercise/application/yiran/app/request/adminRequest"
	"exercise/application/yiran/app/service/admin"
	"github.com/gin-gonic/gin"
)

type userController struct {
}

// Detail 列表
func (u *userController) Detail(ctx *gin.Context) {
	request := new(adminRequest.DetailsRequest)
	err := libs.BindRequest(ctx, request)
	if err != nil {
		libs.Toast(ctx, err.Error())
		return
	}

	ret, err := admin.NewUserService().Detail(ctx, request)
	if err != nil {
		libs.Toast(ctx, err.Error())
		return
	}

	libs.Success(ctx, ret)
	return
}

// Add 新增
func (u userController) Add(ctx *gin.Context) {
	request := new(adminRequest.AddUserRequest)
	err := libs.BindRequest(ctx, request)
	if err != nil {
		libs.Toast(ctx, err.Error())
		return
	}

	/*ret, err := admin.NewUserService().AddSync(ctx, request)*/
	ret, err := admin.NewUserService().Add(ctx, request)
	if err != nil {
		libs.Toast(ctx, err.Error())
		return
	}

	libs.Success(ctx, ret)
	return

}

// Edit 编辑
func (userController) Edit(ctx *gin.Context) {
	request := new(adminRequest.EditUserRequest)
	err := libs.BindRequest(ctx, request)
	if err != nil {
		libs.Toast(ctx, err.Error())
		return
	}

	ret, err := admin.NewUserService().Edit(ctx, request)
	if err != nil {
		libs.Toast(ctx, err.Error())
		return
	}

	libs.Success(ctx, ret)
	return
}

// ListJoin 演示join查询
func (userController) ListJoin(ctx *gin.Context) {

	ret, err := admin.NewUserService().ListJoin(ctx)
	if err != nil {
		libs.Toast(ctx, err.Error())
		return
	}

	libs.Success(ctx, ret)
	return

}

func NewUserController() *userController {
	return &userController{}
}
